package middleware

import (
	kdaocms "longblog/work/dao/cms"
	kutils "longblog/work/utils"
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

func CheckPermissionIsOk(c *gin.Context) bool {
	session := sessions.Default(c)
	userId := session.Get("user")
	if userId == nil {
		return false
	}
	roleIds := kdaocms.GetCmsAdminRoleHasPermissionsByRaw(nil, c.Request.URL.Path)
	roleId := kdaocms.GetCmsAdminUserHasRoleByRaw(nil, kutils.AssertInt64(userId))
	count := len(roleIds)
	for i := 0; i < count; i++ {
		if roleIds[i] == roleId {
			return true
		}
	}
	return false
}
